<template>
  <div class="overview">
    <ul class="static">
      <li v-for="(item, index) in countData" :key="index">
        <p class="static-title">{{ item.title }}</p>
        <p class="static-text">PENDING REVIEW</p>
        <div class="counts">
          <span class="count">{{ item.count }}</span>
          <img src="@/assets/images/quxiaojine.png" />
        </div>
      </li>
    </ul>

    <div class="message">
      <p class="title">消息通知</p>
      <ul>
        <li v-for="item in overvieData.list" :key="item.messageId">
          <div class="message_info">
            <span :class="['tag', { account: item.messageAuditType === 'CUSTOMERACCOUNT' || item.messageAuditType === 'SUPPLIERACCOUNT' || item.messageAuditType === 'SUPPLIER_QUALIFICATION_SOON_EXPIRE' || item.messageAuditType === 'CUSTOMER_QUALIFICATION_SOON_EXPIRE' || item.messageAuditType === 'GOODS_QUALIFICATION_SOON_EXPIRE' }, { cate: item.messageAuditType === 'CUSTOMERQUALIFICATION' || item.messageAuditType === 'SUPPLIERQUALIFICATION' }, { good: item.messageAuditType === 'GOODS' }, { pay: item.messageAuditType === 'ORDERPAY' }]">{{
              $formater.formaterMessageAudit(item.messageAuditType)
            }}</span>
            <span class="info">{{ item.message }}</span>

            <Link v-if="item.messageAuditType === 'SUPPLIER_QUALIFICATION_SOON_EXPIRE' || item.messageAuditType === 'CUSTOMER_QUALIFICATION_SOON_EXPIRE' || item.messageAuditType === 'GOODS_QUALIFICATION_SOON_EXPIRE'" label="前往证件" class="oper" @click="() => onAuditClick(item.messageAuditType)" />
            <Link v-else label="前往审核" class="oper" @click="() => onAuditClick(item.messageAuditType)" />
          </div>
        </li>
      </ul>

      <Pager :page="overvieData.search.page" :pageSizes="overvieData.search.pageSizes" :total="overvieData.total" :limit="overvieData.search.limit" @current-change="onCurrentChange" @size-change="onSizeChange" />
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted, nextTick, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import api from '@/api'
import consts from '@/consts'
import formater from '@/formater'
import { menuStore } from '@/store/menu'
import Link from '@/components/button/link.vue'
import Pager from '@/components/pager/index.vue'

const _menuStore = menuStore()

const router = useRouter()

const overvieData = reactive({
  total: 0,
  list: [],
  search: {
    page: 1,
    limit: consts.Limit,
    pageSizes: consts.PageSize
  }
})

const authority = reactive({
  goodsAudit: false,
  payAudit: false,
  supplierAccountAudit: false,
  customerAccountAudit: false,
  supplierCateAudit: false,
  customerCateAudit: false,
  goodsLicense: false,
  supplierLicense: false,
  customerLicense: false
})

const countData = [
  {
    title: '待审核供应商',
    name: 'supplierCount',
    count: '-'
  },
  {
    title: '待审核采购商',
    name: 'customerCount',
    count: '-'
  },
  {
    title: '待审核订单',
    name: 'orderCount',
    count: '-'
  },
  {
    title: '待审核商品',
    name: 'goodsCount',
    count: '-'
  }
]

const loadData = async () => {
  let { data, err } = await api.get('/medinstrument/boss/message/getMessage', overvieData.search)
  if (err) {
    ElMessage.error(err)
  } else {
    countData?.forEach((e) => {
      e.count = data?.[e.name]
    })

    overvieData.list = data.messageList
    overvieData.total = data.messageCount
  }
}

const onAuditClick = (type) => {
  if (type === 'ORDERPAY' && authority.payAudit) {
    router.push('order/pay')
  } else if (type === 'CUSTOMERQUALIFICATION' && authority.customerCateAudit) {
    router.push('customer/cate')
  } else if (type === 'SUPPLIERQUALIFICATION' && authority.supplierCateAudit) {
    router.push('supplier/cate')
  } else if (type === 'GOODS' && authority.goodsAudit) {
    router.push('goods/audit')
  } else if (type === 'CUSTOMERACCOUNT' && authority.customerAccountAudit) {
    router.push('customer/account')
  } else if (type === 'SUPPLIERACCOUNT' && authority.supplierAccountAudit) {
    router.push('supplier/account')
  } else if (type === 'GOODS_QUALIFICATION_SOON_EXPIRE' && authority.goodsLicense) {
    router.push('goods/license')
  } else if (type === 'CUSTOMER_QUALIFICATION_SOON_EXPIRE' && authority.customerLicense) {
    router.push('customer/license')
  } else if (type === 'SUPPLIER_QUALIFICATION_SOON_EXPIRE' && authority.supplierLicense) {
    router.push('supplier/license')
  } else {
    ElMessage.error('没有审核权限')
    router.push('')
  }
}

const onCurrentChange = (val) => {
  overvieData.search.page = val
  loadData()
}

const onSizeChange = (val) => {
  overvieData.search.limit = val
  loadData()
}

onMounted(() => {
  loadData()

  authority.goodsAudit = !!_menuStore.hasAuthorize('/goods/audit')
  authority.payAudit = !!_menuStore.hasAuthorize('/order/pay')
  authority.supplierAccountAudit = !!_menuStore.hasAuthorize('/supplier/account')
  authority.customerAccountAudit = !!_menuStore.hasAuthorize('/customer/account')
  authority.supplierCateAudit = !!_menuStore.hasAuthorize('/supplier/cate')
  authority.customerCateAudit = !!_menuStore.hasAuthorize('/customer/cate')
  authority.goodsLicense = !!_menuStore.hasAuthorize('/goods/license')
  authority.supplierLicense = !!_menuStore.hasAuthorize('/supplier/license')
  authority.customerLicense = !!_menuStore.hasAuthorize('/customer/license')
})
</script>
<style scoped lang="scss">
.overview {
  padding: 0 15px 0 10px;
  box-sizing: border-box;

  .static {
    height: 110px;
    display: flex;
    justify-content: space-between;
    margin: 10px 0;

    li {
      flex: 1;
      margin-right: 10px;
      height: 110px;
      background-color: #fff;
      border-radius: 4px;
      padding: 18px;
      box-sizing: border-box;

      .static-title {
        height: 16px;
        line-height: 16px;
        font-size: 16px;
        color: var(--color-text);
        font-weight: 500;
      }

      .static-text {
        font-size: 12px;
        color: #dcdfe6;
        line-height: 16px;
        margin: 5px 0 10px 0;
      }

      .counts {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: 10px;

        .count {
          display: inline-block;
          font-size: 28px;
          color: #0486fe;
          font-weight: 500;
          line-height: 28px;
        }

        img {
          width: 30px;
          height: 30px;
        }
      }
    }

    li:last-child {
      margin-right: 0;
    }
  }

  .message {
    padding: 20px 0 30px 20px;
    margin-top: 10px;
    background-color: #fff;
    .title {
      color: var(--color-primary-text);
      font-size: 16px;
      line-height: 16px;
      font-weight: 500;
    }
    .message_info {
      margin-top: 20px;

      span {
        display: inline-block;
      }

      .tag {
        height: 24px;
        line-height: 16px;
        padding: 4px;
        margin-right: 10px;
        border-radius: 4px;
        box-sizing: border-box;

        &.account {
          background-color: #feeef0;
          color: #e88563;
        }

        &.cate {
          background-color: #eeeefe;
          color: #6381e8;
        }

        &.good {
          background-color: #eefbfe;
          color: #498cef;
        }

        &.pay {
          background-color: #fff7e9;
          color: #e49843;
        }
      }

      .info {
        color: var(--color-text);
        height: 16px;
        line-height: 16px;
      }

      .oper {
        margin-left: 10px;

        a {
          color: #0486fe;
          font-weight: 500;
        }
      }
    }
  }
}
</style>
